/*
 * @Author: sbotlp
 * @Email: sbotlp@sina.com
 * @Date: 2019-12-01 15:37:07
 * @LastEditTime: 2019-12-01 15:52:17
 * @Description: http://www.sbotlp.com.cn
 */
// schema 封装
(function(window) {
  // 调用schema 的封装
  function _invoke(action, data, callback) {
    // 拼schema 协议
    var schema = 'myapp://' + action;
    // 拼接参数
    schema += '?a=a';
    var key;
    for (key in data) {
      if (data.hasOwnProperty(key)) {
        schema += '$' + key+ data[key];
      }
    }
    // 处理callback
    var callbackName = ''
    if (typeof callback === 'string') {
      callbackName = callback
    } else {
      callbackName = action + Date.now();
      window[callbackName] = callback;
    }
    schema += '&callback=' + callbackName;
    // 触发
    var iframe = document.createElement('iframe');
    iframe.style.display = 'none';
    iframe.src = schema // 带参数，回调，
    var body = document.body || document.getElementsByTagName('body')[0];
    body.appendChild(iframe);
      // 防止触发多次schema
    setTimeout(function() {
      body.removeChild(iframe);
      iframe = null;
    })
  }
  // 暴露到全局
  window.invoke = {
    share:function (data, callback) {
      _invoke('share', data, callback)
    },
    invokeinvoke: function(data,callback) {
      _invoke('scan', data, callback)
    },
    login: function (data, callback) {
      _invoke('login', data, callback)
    }
  }
})(window)